<?php

class FriendLinkCatController extends BaseController
{

	/**
	 * 分类管理
	 */
	public function actionAdmin()
	{
		$model=new Albums('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = Company::model()->getCmpUserId();
		if(isset($_GET['Albums']))
			$model->attributes=$_GET['Albums'];
		
		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * 载入模型
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Albums::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * ajax验证
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cmp-cat-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 * 新建分类
	 */
	public function actionCreate()
	{
		$model=new CmpCat;
	
		if(isset($_GET['zpc_catname']))
		{
			$catname = trim($_GET['zpc_catname']);
			$prentid = trim($_GET['prentid']);
			$zpc_id = CmpCat::model()->findByAttributes(array('zpc_catname'=>$catname));
			

			//查询顶级分类信息
			$prentcatinfo = CmpCat::model()->findByPk($prentid);
			$topid = 0;
			$childid = null;
			if(!empty($prentcatinfo)){
				$topid   = $prentcatinfo->zpc_parentid;
				$childid = $prentcatinfo->zpc_childid;
			}
			$model->zpc_catname = $catname;
			$model->zpc_parentid = $prentid;
			$model->zpc_topid = $topid;
			$model->zpc_type = 6;//类型：6链接分类
			$model->zm_id = Company::model()->getCmpUserId();
			$model->save();
			//修改父级分类的zpc_childid
			if(!empty($prentcatinfo)){
				if(isset($childid)){
					$prentcatinfo->zpc_childid = $childid.','.$model->zpc_id;
				}else{
					$prentcatinfo->zpc_childid =$model->zpc_id;
				}
				if(!$prentcatinfo->save()){
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，创建分类失败，请稍后再试！'));
				exit;
			}
			}
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}else{
			throw new CHttpException("参数出错");
		}
	
	}
	
	/**
	 * 保存操作
	 */
	public function actionSave(){
		$catname = $_POST['data']['zpc_catname'];
		$newcatname = array_unique($_POST['data']['zpc_catname']);
		$trimcatname = array_filter($_POST['data']['zpc_catname']);
		//判断分类是否存在重复
		if(count($catname) != count($newcatname)){
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，创建分类失败，类名称不能有重复！'));
			exit;
		}
		//判断分类名称是否存在空值
		if(count($catname) != count($trimcatname)){
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，创建分类失败，分类名称不能为空！'));
			exit;
		}
		foreach ($_POST['data']['zpc_id'] as $key => $val){
			$model = CmpCat::model()->findByPk($val);
			$model->zpc_catname   = trim($_POST['data']['zpc_catname'][$key]);
			$model->zpc_listorder = $key;
			if(!$model->save()){
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，保存失败，请稍后再试！'));
				exit;
			}
		}
		//成功跳转 
		$this->redirect(Yii::app()->request->urlReferrer);
	}
	
	/**
	 * 删除分类
	 */
	public function actionDelete($id)
	{
		$model = CmpCat::model()->findByPk($id);
		//判断该分类下面有没有子分类，有子分类不能进行删除
		if(!isset($model->zpc_childid)){
			//判断该分类下面有没有产品
			$zmpPro = CmpCatlog::model()->findByAttributes(array('zpc_id'=>$id));
			if(isset($zmpPro->zpc_id)){
				//失败跳转
				$this->render('../index/error',array('message'=>'该分类下面有链接，请先删除链接！'));
				exit;
			}
			//先修改父级分类下面的子分类id
			$parentinfo 	= CmpCat::model()->findByPk($model->zpc_parentid);
			if(isset($parentinfo->zpc_childid)){
				$childid 		= explode(',',$parentinfo->zpc_childid);
				unset($childid[array_search($id,$childid)]);
				$parentinfo->zpc_childid = implode(',',$childid);
				$parentinfo->save();
			}
			if($model->delete()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，删除失败，请稍后再试！'));
				exit;
			}
		}else{
			//失败跳转
			$this->render('../index/error',array('message'=>'请确保该分类下面没有子分类'));
			exit;
		}
	}
	
}
